package com.sicheng.algorithm.array;

import com.sicheng.algorithm.common.Sort;
import com.sicheng.metadata.se.exp.first.IsOdd;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2021/9/16 8:09
 */
public class OddAdjust {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8};
        oddAdjust(nums);
        System.out.println(Arrays.toString(nums));
    }

    public static void oddAdjust(int[] nums) {
        if (nums != null && nums.length > 0) {
            int left = 0, right = nums.length - 1;
            while (left < right) {
                while (left < right) {
                    if (IsOdd.isOdd(nums[right]) && IsOdd.isOdd(nums[left])) {
                        left++;
                    } else break;
                }
                while (left < right) {
                    if (!IsOdd.isOdd(nums[right]) && !IsOdd.isOdd(nums[left])) {
                        right--;
                    } else break;
                }


                while (left < right) {
                    if ((IsOdd.isOdd(nums[right]) && !IsOdd.isOdd(nums[left]))) {
                        Sort.swap(nums, left, right);
                        right--;
                        left++;
                    } else break;
                }

                while (left < right) {
                    if (IsOdd.isOdd(nums[left]) && !IsOdd.isOdd(nums[right])) {
                        right--;
                        left++;
                    } else break;
                }
            }

        }
    }
}
